//
//  TagePageDefileTableViewController.m
//  PetTown
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "TagePageDefileTableViewController.h"
#import "MJRefresh.h"
@interface TagePageDefileTableViewController ()
{
    MBProgressHUD * mbBar;
}
@end

@implementation TagePageDefileTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allDataArray = [NSMutableArray array];//创建model对象数组
   
    _page = 1;
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //上啦刷新
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    //设置标题
    self.navigationItem.title = _itemTitle;
    
    //设置背景图片
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImg.png"]];
    
    //设置
    [self readData];//解析数据
}

//下拉刷新
- (void)headerRereshing{
    
    [self.tableView headerEndRefreshing];
}
//上拉刷新
-(void)footerRereshing{
    _page++;
    [self readData];
    [self.tableView footerEndRefreshing];
}

//读取数据
- (void)readData{
    
    mbBar = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.tableView addSubview:mbBar];
    mbBar.labelText = @"正在加载..";
    [mbBar show:YES];
    
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:BelleTagChoicenessPageAPI, [NSString stringWithFormat:@"%d",_page],_tag_id]];
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    

    __weak TagePageDefileTableViewController * tagPDTVC = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (nil == data) {
            return ;
        }
        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * array = [dic objectForKey:@"data"];
       
        for (NSDictionary * modelDic in array) {
            HomePage * homePage = [[HomePage alloc] init];
            [homePage setValuesForKeysWithDictionary:modelDic];
            [tagPDTVC.allDataArray addObject:homePage];
        }
        [self.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _allDataArray.count;
}

//设置cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChoicenessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if (!cell) {
        cell = [[ChoicenessCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"mycell"];
    }
    HomePage * hh = _allDataArray[indexPath.row];
    cell.homePage = hh;
    if (hh.comments.count != 0) {
        self.ReviewerArray = [[NSMutableArray alloc] init];
        for (NSDictionary * dd in hh.comments) {
            Reviewer * rr = [[Reviewer alloc] init];
            [rr setValuesForKeysWithDictionary:dd];
            [_ReviewerArray addObject:rr];
            cell.reviewer = _ReviewerArray[0];
        }
        
    }
    
    [MBProgressHUD hideHUDForView:self.tableView animated:YES];
    
    return cell;
}
//点击cell弹出详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomePageNewTableViewController * homePageNewTVC = [[HomePageNewTableViewController alloc] init];
    HomePage * hh = _allDataArray[indexPath.row];
    homePageNewTVC.homePage = hh;
    [self.navigationController pushViewController:homePageNewTVC animated:YES];
    
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomePage * homePage = _allDataArray[indexPath.row];
    CGFloat b = self.tableView.frame.size.width * [homePage.picheight floatValue] / [homePage.picwidth floatValue] + 280;
    return b;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
